//
//  FriendListMoreView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/28.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "FriendListMoreView.h"

@interface  FriendListMoreView()

@property (nonatomic, strong) UIImageView *menu_bg_view;
@property (nonatomic, strong) UIButton *remark_button;
@property (nonatomic, strong) UIButton *delete_button;

@property (nonatomic, weak) id<FriendListMoreViewDelegate>delegate;
@property (nonatomic, strong) FriendModel *friendModel;

@end

@implementation FriendListMoreView

+ (FriendListMoreView *)sharedMore
{
    static FriendListMoreView *moreView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        moreView = [[self alloc] init];
    });
    return moreView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = RGBA(0, 0, 0, 0.3);
        
        [self makeMenuBGView];
        
        [self makeMenuButton];
    }
    return self;
}

- (void)makeMenuBGView
{
    _menu_bg_view = [UIImageView new];
    _menu_bg_view.userInteractionEnabled = YES;
    _menu_bg_view.image = [UIImage imageNamed:@"friend_menu_bg_image"];
    [self addSubview:_menu_bg_view];
}

- (void)makeMenuButton
{
    _remark_button = [UIButton new];
    [_remark_button setTitle:@"添加备注" forState:UIControlStateNormal];
    [_remark_button setTitleColor:RGB(88, 88, 88) forState:UIControlStateNormal];
    _remark_button.backgroundColor = [UIColor whiteColor];
    _remark_button.layer.cornerRadius = 5.0;
    _remark_button.layer.shadowColor = RGB(232, 232, 232).CGColor;//shadowColor阴影颜色
    _remark_button.layer.shadowOffset = CGSizeMake(0,5);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    _remark_button.layer.shadowOpacity = 0.8;
    _remark_button.layer.shadowRadius = 1;
    [_remark_button addTarget:self action:@selector(clickRemarkButton:) forControlEvents:UIControlEventTouchUpInside];
    [_menu_bg_view addSubview:_remark_button];
    
    
    _delete_button = [UIButton new];
    [_delete_button setTitle:@"删除好友" forState:UIControlStateNormal];
    [_delete_button setTitleColor:RGB(88, 88, 88) forState:UIControlStateNormal];
    _delete_button.backgroundColor = RGB(240, 104, 105);
    _delete_button.layer.cornerRadius = 5.0;
    _delete_button.layer.shadowColor = RGB(218, 95, 96).CGColor;//shadowColor阴影颜色
    _delete_button.layer.shadowOffset = CGSizeMake(0,4);//shadowOffset阴影偏移,x向右偏移4，y向下偏移4，默认(0, -3),这个跟shadowRadius配合使用
    _delete_button.layer.shadowOpacity = 1;
    _delete_button.layer.shadowRadius = 1;
    [_delete_button addTarget:self action:@selector(clickDeleteButton:) forControlEvents:UIControlEventTouchUpInside];
    [_menu_bg_view addSubview:_delete_button];
}

- (void)showMoreWithRect:(CGRect)rect friend:(FriendModel *)friendModel delegate:(id<FriendListMoreViewDelegate>)delegate
{
    self.delegate = delegate;
    
    self.friendModel = friendModel;
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [self settingLayoutWithRect:rect];
}

- (void)settingLayoutWithRect:(CGRect)rect
{
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
    
    [_menu_bg_view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-88);
        make.top.mas_equalTo(rect.origin.y+56);
        make.size.mas_equalTo(CGSizeMake(165, 108));
    }];
    
    [_remark_button mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(8.7);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(39);
    }];
    
    [_delete_button mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-10);
        make.left.mas_equalTo(8.7);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(39);
    }];
}

- (void)clickRemarkButton:(UIButton *)button
{
    [self hide];
    if ([self.delegate respondsToSelector:@selector(didSelectRemarkWithFriend:)]) {
        [self.delegate didSelectRemarkWithFriend:self.friendModel];
    }
}

- (void)clickDeleteButton:(UIButton *)button
{
    [self hide];
    if ([self.delegate respondsToSelector:@selector(didSelectDelegateWithFriend:)]) {
        [self.delegate didSelectDelegateWithFriend:self.friendModel];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hide];
}

- (void)hide
{
    [self removeFromSuperview];
}

@end
